package com.sczmgk.hotline.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sczmgk.hotline.R;

public class MyHeadView extends LinearLayout {

    /**
     * 标题
     */
    private TextView mTextViewTitle;

    /**
     * 标题icon
     */
    private TextView mTitleIcon;
    /**
     * 左边按钮
     */
    private Button mBtnLeft;

    /**
     * 右边按钮
     */
    private Button mBtnRight;

    /**
     * 整个View
     */
    private View mHeadView;
    /**
     * 新消息的红点
     */
    private ImageView newMsgPoint;

    /**
     * 当前左边按钮采用的背景图片
     */
    private int mBtnLeftBGResource = -1;
    /**
     * 当前右边按钮采用的背景图片
     */
    private int mBtnRightBGResource = -1;

    /**
     * 当前右边按钮采用的背景图片
     */
    private int mBtnNextRightBGResource = -1;

    /**
     * 当前左边按钮采用的背景图片
     */
    private int mBtnLeftTextResource = -1;

    /**
     * 当前右边按钮采用的背景图片
     */
    private int mBtnRightTextResource = -1;

    /**
     * 当前右边按钮采用的背景图片
     */
    private int mBtnNextRightTextResource = -1;

    private Drawable icon;

    private Button mBtnLeftClose;
    private Button mBtnRightNext;
    private Button mBtnShare;
    private TextView mTitleText;
    private TextView mTitleSingle;
    private LinearLayout rightTitleContainer;

    public MyHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public int getRightBtnTextResource() {
        return mBtnRightTextResource;
    }

    public int getNextRightBtnTextResource() {
        return mBtnNextRightTextResource;
    }

    public int getLeftBtnTextResource() {
        return mBtnLeftTextResource;
    }

    private FrameLayout mLeftFl;

    private String TAG = "MyHeadView";

    /**
     * 初始化View
     *
     * @param context
     */
    public void initView(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        mHeadView = layoutInflater.inflate(R.layout.layout_head, null);

        addView(mHeadView);

        mTextViewTitle = (TextView) mHeadView.findViewById(R.id.headerView);
        mTitleIcon = (TextView) mHeadView.findViewById(R.id.title_icon);
        mBtnLeft = (Button) mHeadView.findViewById(R.id.btn_left_title);
        mLeftFl = (FrameLayout) mHeadView.findViewById(R.id.linearlayout_left_title);
        mBtnRight = (Button) mHeadView.findViewById(R.id.btn_right_title);
        mBtnRightNext = (Button) mHeadView.findViewById(R.id.btn_right_title_next);
        icon = getResources().getDrawable(R.drawable.notify_icon);
        mBtnLeftClose = (Button) mHeadView.findViewById(R.id.btn_close_title);
        newMsgPoint = (ImageView) mHeadView.findViewById(R.id.iv_new_msg);
        mBtnShare = (Button) mHeadView.findViewById(R.id.btn_right_share);
        mTitleText = (TextView) mHeadView.findViewById(R.id.title_text);
        mTitleSingle = (TextView) mHeadView.findViewById(R.id.title_single_text);
        rightTitleContainer = (LinearLayout) mHeadView.findViewById(R.id.linearlayout_right_title);

        //字体加粗
        TextPaint tp = mTextViewTitle.getPaint();
        tp.setFakeBoldText(true);
    }

    /**
     * 显示新消息的红点
     */
    public void showNewMsgPoint() {
        newMsgPoint.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏新消息的红点
     */
    public void goneNewMsgPoint() {
        newMsgPoint.setVisibility(View.GONE);
    }

    /**
     * 获取左边按钮的背景资源ID
     *
     * @return
     */
    public int getLeftBtnBGResource() {
        return mBtnLeftBGResource;
    }

    /**
     * 设置左边按钮显示状态
     *
     * @param visible
     */
    public void setLeftBtnVisibility(int visible) {
        mBtnLeft.setVisibility(visible);
    }

    /**
     * 设置左边按钮背景
     *
     * @param resource
     */
    public void setLeftBtnBackground(int resource) {
        setLeftBtnBackground(resource, -1);
    }

    /**
     * 设置分享按钮背景(先写死)
     */
    public void setShareBtnBackground(int resource) {
        //TODO
        mBtnShare.setBackgroundResource(resource);
    }

    /**
     * 设置分享按钮显示状态
     */
    public void setShareBtnVisibility(int visible) {
        //TODO
        mBtnShare.setVisibility(visible);
    }

    /**
     * 设置分享按钮的点击事件
     * @param listener
     */
    public void setShareBtnClickListener(OnClickListener listener) {
        mBtnShare.setOnClickListener(listener);
    }

    /**
     * 设置左边按钮背景
     */
    public void setLeftBtnBackground(int resource, int text) {
        mBtnLeft.setBackgroundResource(resource);
        mBtnLeftBGResource = resource;
        mBtnLeftTextResource = text;
        if (text > 0) {
            mBtnLeft.setText(text);
        } else {
            mBtnLeft.setText("");
        }
    }

    /**
     * 设置左边按钮背景
     *
     * @param resource
     */
    public void setLeftBtnBackground(int resource, int text, int textColor) {
        if (resource >= 0) {
            mBtnLeft.setBackgroundResource(resource);
        }
        if (textColor >= 0) {
            mBtnLeft.setTextColor(getResources().getColor(textColor));
        }
        mBtnLeftBGResource = resource;
        mBtnLeftTextResource = text;
        if (text > 0) {
            mBtnLeft.setText(text);
        } else {
            mBtnLeft.setText("");
        }
    }

    /**
     * 设置左边按钮背景
     *
     * @param textColor
     */
    public void setLeftBtnTextColor(int textColor) {
        if (textColor >= 0) {
            mBtnLeft.setTextColor(getResources().getColor(textColor));
        }
    }

    /**
     * 设置左边按钮的文字
     */
    public void setLeftBtnText(String text) {
        mBtnLeft.setText(text);
    }

    /**
     * 设置左边按钮背景 一旦为左边按钮设置监听，将会导致无法切换menu菜单
     *
     * @param listener
     */
    public void setLeftBtnClickListener(OnClickListener listener) {
        mBtnLeft.setOnClickListener(listener);
        mLeftFl.setOnClickListener(listener);
    }

    /**
     * 设置左边按钮背景 一旦为左边按钮设置监听，将会导致无法切换menu菜单
     */
    public void setRightTitleClickListener(OnClickListener listener) {
        rightTitleContainer.setOnClickListener(listener);
    }

    /**
     * 设置右边按钮显示状态
     *
     * @param visible
     */
    public void setRightBtnVisibility(int visible) {
        mBtnRight.setVisibility(visible);
    }

    /**
     * 设置右边按钮背景
     *
     * @param resource
     */
    public void setRightBtnBackground(int resource) {
        setRightBtnBackground(resource, -1);
    }

    /**
     * 设置右边按钮背景
     *
     * @param resource
     */
    public void setRightBtnBackground(int resource, int text) {
        mBtnRight.setBackgroundResource(resource);
        mBtnRightBGResource = resource;
        mBtnRightTextResource = text;
        if (text > 0) {
            mBtnRight.setText(text);
        } else {
            mBtnRight.setText("");
        }
    }

    /**
     * 设置右边第二个按钮显示状态
     *
     * @param visible
     */
    public void setNextRightBtnVisibility(int visible) {
        mBtnRightNext.setVisibility(visible);
    }

    /**
     * 设置右边第二个按钮背景
     *
     * @param resource
     */
    public void setNextRightBtnBackground(int resource) {
        setNextRightBtnBackground(resource, -1);
    }

    /**
     * 设置右边第二个按钮背景
     *
     * @param resource
     */
    public void setNextRightBtnBackground(int resource, int text) {
        mBtnRightNext.setBackgroundResource(resource);
        mBtnNextRightBGResource = resource;
        mBtnNextRightTextResource = text;
        if (text > 0) {
            mBtnRightNext.setText(text);
        } else {
            mBtnRightNext.setText("");
        }
    }

    /**
     * 设置右边第二个按钮背景
     *
     * @param resource
     */
    public void setNextRightBtnBackground(int resource, String text) {
        mBtnRightNext.setBackgroundResource(resource);
        mBtnNextRightBGResource = resource;
        mBtnNextRightTextResource = -1;

        if (text != null) {
            mBtnRightNext.setText(text);
        }
    }

    /**
     * 设置右边按钮背景
     *
     * @param resource
     */
    public void setRightBtnBackground(int resource, String text) {
        mBtnRight.setBackgroundResource(resource);
        mBtnRightBGResource = resource;
        mBtnRightTextResource = -1;

        if (text != null) {
            mBtnRight.setText(text);
        }
    }

    /**
     * 设置右边第二个按钮背景
     *
     * @param resource
     */
    public void setNextRightBtnBackground(int resource, int text, int textColor) {
        if (resource >= 0) {
            mBtnRightNext.setBackgroundResource(resource);
        }
        if (textColor >= 0) {
            mBtnRightNext.setTextColor(getResources().getColor(textColor));
        }
        mBtnNextRightBGResource = resource;
        mBtnNextRightTextResource = text;
        if (text > 0) {
            mBtnRightNext.setText(text);
        } else {
            mBtnRightNext.setText("");
        }
    }

    /**
     * 设置右边按钮背景
     *
     * @param textColor
     */
    public void setRightBtnTextColor(int textColor) {
//    	MyLog.d(TAG ,"准备    设置title文字的颜色 ： "+textColor);
        if (textColor >= 0) {
//        	MyLog.d(TAG ,"设置title文字的颜色 ： "+textColor);
            mBtnRight.setTextColor(getResources().getColor(textColor));
        }
    }

    /**
     * 设置右边按钮的文字
     *
     * @param text
     */
    public void setRightBtnText(String text) {
        mBtnRight.setText(text);
    }

    /**
     * 设置右边按钮文字
     */
    public void setRightBtnText(int resid) {
        mBtnRight.setText(resid);
    }


    /**
     * 设置右边按钮描述
     */
    public void setRightBtnDesc(String desc) {
        mBtnRight.setContentDescription(desc);
    }

    /**
     * 设置左边按钮背景 一旦为左边按钮设置监听，将会导致无法切换menu菜单
     *
     * @param listener
     */
    public void setRightBtnClickListener(OnClickListener listener) {
        mBtnRight.setOnClickListener(listener);
    }

    /**
     * 设置右边按钮背景
     *
     * @param resource
     */
    public void setRightBtnBackground(int resource, int text, int textColor) {
        if (resource >= 0) {
            mBtnRight.setBackgroundResource(resource);
        }
        if (textColor >= 0) {
            mBtnRight.setTextColor(getResources().getColor(textColor));
        }
        mBtnRightBGResource = resource;
        mBtnRightTextResource = text;
        if (text > 0) {
            mBtnRight.setText(text);
        } else {
            mBtnRight.setText("");
        }
    }

    /**
     * 设置右边第二个按钮背景
     *
     * @param textColor
     */
    public void setNextRightBtnTextColor(int textColor) {
//    	MyLog.d(TAG ,"准备    设置title文字的颜色 ： "+textColor);
        if (textColor >= 0) {
//        	MyLog.d(TAG ,"设置title文字的颜色 ： "+textColor);
            mBtnRightNext.setTextColor(getResources().getColor(textColor));
        }
    }

    /**
     * 设置右边第二个按钮的文字
     *
     * @param text
     */
    public void setNextRightBtnText(String text) {
        mBtnRightNext.setText(text);
    }

    /**
     * 设置右边第二个按钮文字
     */
    public void setNextRightBtnText(int resid) {
        mBtnRightNext.setText(resid);
    }

    /**
     * 设置右边第二个按钮描述
     */
    public void setNextRightBtnDesc(String desc) {
        mBtnRightNext.setContentDescription(desc);
    }

    /**
     * 设置左边按钮背景 一旦为左边按钮设置监听，将会导致无法切换menu菜单
     *
     * @param listener
     */
    public void setNextRightBtnClickListener(OnClickListener listener) {
        mBtnRightNext.setOnClickListener(listener);
    }


    /**
     * 获取右边第二个按钮的背景资源ID
     *
     * @return
     */
    public int getRightBtnBGResource() {
        return mBtnRightBGResource;
    }

    /**
     * 获取右边第二个按钮的背景资源ID
     *
     * @return
     */
    public int getNextRightBtnBGResource() {
        return mBtnNextRightBGResource;
    }

    /**
     * 设置标题
     *
     * @param resource
     */
    public void setTitle(int resource) {
        mTextViewTitle.setText(resource);
    }

    /**
     * 设置标题 icon
     *
     * @param resource
     */
    public void setTitleIcon(int resource) {
        if (resource >= 0) {
            // mTextViewTitle.setCompoundDrawablesWithIntrinsicBounds(null,
            // null, icon, null);
            mTitleIcon.setBackgroundResource(resource);
            mTitleIcon.setVisibility(View.VISIBLE);
        } else {
            mTextViewTitle.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            mTitleIcon.setVisibility(View.GONE);
        }

    }

    public void setTitleIconText(int resource){
        mTitleText.setText(resource);
    }

    public void setTitleIconText(String textDes){
        mTitleText.setText(textDes);
    }

    public String getTitleIconText(){
        return mTitleText.getText().toString().trim();
    }

    public void setTitleIconVisiable(int visiable){
        mTitleText.setVisibility(visiable);
    }

    public void setTitleIconClick(OnClickListener listener){
        mTitleText.setOnClickListener(listener);
    }

    public void setTitleText(int resource){
        mTitleSingle.setText(resource);
    }

    public void setTitleText(String textDes){
        mTitleSingle.setText(textDes);
    }

    public void setTitleVisiable(int visiable){
        mTitleSingle.setVisibility(visiable);
    }

    public void setTitleBackground(int resource){
        mTitleSingle.setBackgroundResource(resource);
    }

    public void setTitleClick(OnClickListener listener){
        mTitleSingle.setOnClickListener(listener);
    }

    public void setTitleTextColor(int res){
        mTitleSingle.setTextColor(res);
    }

    public void setTitleTextFoucse(boolean isFouces){
        mTitleSingle.setClickable(isFouces);
    }


    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        mTextViewTitle.setText(title);
    }

    /**
     * 设置标题颜色
     *
     * @param color 标题颜色ID
     */
    public void setTitleColor(int color) {
        mTextViewTitle.setTextColor(color);
    }

    public Button getBtnLeft() {
        return mBtnLeft;
    }

    public Button getBtnRight() {
        return mBtnRight;
    }

    public Button getNextBtnRight() {
        return mBtnRightNext;
    }

    /**
     * 设置头部背景
     *
     * @param color 背景资源ID
     */
    public void setHeadBackground(int color) {
        mHeadView.setBackgroundColor(color);
    }

    public void setLeftCloseBtnVisibility(int visible) {
        mBtnLeftClose.setVisibility(visible);
    }

    public void setLeftCloseBtnClickListener(OnClickListener listener) {
        mBtnLeftClose.setOnClickListener(listener);
    }

}
